mui.plusReady(function() {
	var wgtVer = null;
	var device = null;
	var message;
	var wgtUrl;
	var title = '软件更新';

	function muiinit() {
		device = plus.os.name == 'iOS' ? 1 : 2;
		plus.runtime.getProperty(plus.runtime.appid, function(inf) {
			wgtVer = inf.version;
			mui.ajax({
				type: "post",
				url: 'http://www.jiaoyou0.cn/index.php/index/index/versionUpgrade/manager_id/777',
				async: true,
				data: {
					'version': wgtVer,
					'ios': device
				},
				success: function(data) {
					var abdi = localStorage.getItem('allpay');
					if(abdi != 1) {
						localStorage.setItem('allpay', data.allpay)
					}
					if(data.code == 200) {
						var data = data.data;
						wgtUrl = data.apk_url;
						message = data.upgrade_point;
						if(data.type == 2) {
							modelClose(wgtUrl)
						} else if(data.type == 9) {
							modelUrl(wgtUrl)
						} else {
							model(wgtUrl)
						}
					}
				}
			});

			function model(wgtUrl) {
				var btnArray = ['立即更新', '下次再说'];
				mui.confirm(message, title, btnArray, function(e) {
					if(e.index == 0) {
						downWgt(wgtUrl)
					}
				})
			}

			function modelUrl(wgtUrl) {
				var btnArray = ['立即更新', '下次再说'];
				mui.confirm(message, title, btnArray, function(e) {
					if(e.index == 0) {
						mui.plusReady(function() {
							plus.runtime.openURL(wgtUrl)
						})
					}
				})
			}

			function modelClose(wgtUrl) {
				var btnArray = ['立即更新', '下次再说'];
				mui.confirm(message, title, btnArray, function(e) {
					if(e.index == 0) {
						downWgt(wgtUrl)
					} else {
						plus.runtime.quit()
					}
				})
			}

			function downWgt(wgtUrl) {
				plus.nativeUI.showWaiting("下载文件...");
				plus.downloader.createDownload(wgtUrl, {
					filename: "_doc/update/"
				}, function(d, status) {
					if(status == 200) {
						installWgt(d.filename)
					} else {
						plus.nativeUI.alert("下载失败！")
					}
					plus.nativeUI.closeWaiting()
				}).start()
			}

			function installWgt(path) {
				plus.nativeUI.showWaiting("解压资源文件...");
				plus.runtime.install(path, {}, function() {
					plus.nativeUI.closeWaiting();
					plus.nativeUI.alert("更新完成！", function() {
						plus.runtime.restart()
					})
				}, function(e) {
					plus.nativeUI.closeWaiting();
					plus.nativeUI.alert("升级失败[" + e.code + "]：" + e.message)
				})
			}
		})
	}
	muiinit()
});